'''
Created on 20.5.2012

@author: mphkh
'''
from CashFlowUtils.IceBonds import *
from datetime import date

from numpy import *
from scipy.interpolate import interp1d
from scipy import interpolate
from array import *

class MarketData(object):
    def __init__(self, bidprice, askprice, lastprice, closingprice, newyield=0.0):
        self.bidprice = bidprice
        self.askprice = askprice
        self.lastprice = lastprice
        self.closingprice = closingprice
        self.newyield = newyield

class BondPosition(object):
    def __init__(self, bond, nominal, marketprice):
        self.bond           = bond
        self.nominal        = nominal
        self.marketprice    = marketprice
        
class BondContainer(object):
    def __init__(self, bond, tradeamount, tradeprice, tradedate, settlementdate, tradeid=0):
        self.bond           = bond
        self.tradeamount    = tradeamount
        self.tradeprice     = tradeprice
        self.tradedate      = tradedate
        self.settlementdate = settlementdate
        self.tradeid        = tradeid
        
class CurveObject(object):
    def __init__(self, bond, cleanprice, dirtyprice, maturitydate):
        self.bond           = bond
        self.cleanprice     = cleanprice
        self.dirtyprice     = dirtyprice
        self.maturitydate   = maturitydate
        
mdata = {}

mdata['RIKB 12 0824']=MarketData(100.050,100.350,100.175,100.166,0.0483)
mdata['RIKB 13 0517']=MarketData(102.880,102.985,102.880,102.880,0.0424)
mdata['RIKB 16 1013']=MarketData(100.750,101.050,100.900,100.900,0.0424)
mdata['RIKB 19 0226']=MarketData(109.890,110.300,110.100,110.100,0.068)
mdata['RIKB 22 1026']=MarketData(99.200,99.650,99.425,99.425,0.0732)
mdata['RIKB 25 0612']=MarketData(104.300,104.790,104.770,104.770,0.0741)
mdata['RIKB 31 0124']=MarketData(92.310,92.920,92.500,92.500,0.0724)

settlementdate = date.today()

curveobjects = []

for key in mdata:
    bond = geniusdescrtobond(key)
    curveobjects.append(CurveObject(bond,mdata[key].closingprice, bond.cleantodirty(settlementdate, mdata[key].closingprice),bond.maturitydate))
    
curveobjects.sort(key=lambda point: point.maturitydate)

noco = len(curveobjects)
t = zeros(noco)
y = zeros(noco)

i = 0

activebonddescr = 'RIKB 16 1013'
activebond = geniusdescrtobond(activebonddescr)
abrcfs = activebond.remaining_cashflows(settlementdate)
dirty = activebond.cleantodirty(settlementdate,mdata[activebonddescr].closingprice)

print dirty

'''
Initial curve for guessing
'''
for obj in curveobjects:
    remainingcfs = obj.bond.remaining_cashflows(settlementdate)
    t[i] = remainingcfs[-1].yearfrac
    y[i] = mdata[obj.bond.description].newyield
    i = i+1

err = 100.0

count = 0.0

while err>0.1:
    sumcf = 0.0
    f1 = interp1d(t, y)

    for cf in abrcfs:
        df = (1+f1(cf.yearfrac))**(-cf.yearfrac)
        sumcf = sumcf + df*cf.amount
    print sumcf

    err = sumcf - dirty
    
    print err

    y[2] = y[2]+0.0005
    
    count = count+1
    if count>100:
        err = 0.001

print sumcf
print y[2]    

for cf in abrcfs:
    df = (1+f1(cf.yearfrac))**(-cf.yearfrac)
    sumcf = sumcf + df*cf.amount
    print cf.cfdate,f1(cf.yearfrac),df*cf.amount


